package com.pospi.xstore.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.widget.Toast;

import com.pospi.xstore.R;
import com.pospi.xstore.dto.GoodsDto;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by acer on 2018/6/19.
 */

public class CountSAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsDto> goodsDtos;

    public CountSAdapter(Context context, List<GoodsDto> goodsDtos) {
        this.context = context;
        this.goodsDtos = goodsDtos;
    }

    @Override
    public int getCount() {
        return goodsDtos.size();
    }

    @Override
    public Object getItem(int i) {
        return goodsDtos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final GoodsDto dto = goodsDtos.get(i);
        final Holder holder;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(context).inflate(R.layout.counts_item, null);
            holder.code = (TextView) view.findViewById(R.id.tv_item_code);
            holder.name = (TextView) view.findViewById(R.id.tv_item_name);
            holder.et_num = view.findViewById(R.id.et_goodsnNumber);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.code.setText(dto.getCode());
        holder.name.setText(dto.getName());
        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        holder.et_num.setText(decimalFormat.format(dto.getNum()));
//        holder.et_num.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//                String num = holder.et_num.getText().toString().trim();
//                if (!num.isEmpty()) {
//                    dto.setNum(Double.parseDouble(holder.et_num.getText().toString().trim()));
//                }
//            }
//        });

        return view;
    }
    class Holder{
        TextView code;
        TextView name;
        TextView et_num;
    }
}
